package beans;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class DateDeserializer implements JsonDeserializer<Date> {

	@Override
	public Date deserialize(JsonElement arg0, Type arg1,
			JsonDeserializationContext arg2) throws JsonParseException {
		try {
			SimpleDateFormat format = new SimpleDateFormat(Account.PATTERN_VI);
			Date oldPattern = format.parse(arg0.getAsString());
			format.applyPattern(Account.PATTERN_US);
			Date newPattern = format.parse(format.format(oldPattern));
			// return new SimpleDateFormat(Account.PATTERN_VI).parse(arg0
			// .getAsString());
			return newPattern;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		throw new JsonParseException("Cann't parse date: " + arg0.getAsString());
	}
}
